﻿/*
	Copyright 2008 CATsoft Development GmbH

  Licensed under the Apache License, Version 2.0 (the "License");
  you may not use this file except in compliance with the License.
  You may obtain a copy of the License at

  http://www.apache.org/licenses/LICENSE-2.0

  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  See the License for the specific language governing permissions and
  limitations under the License.
*/
/*
	Options Dialog
*/

/***********************************************************************
	On Open
***********************************************************************/
function options_onOpen(){
	
	
	options.putValue("optionsSaved", 0)	

	COMM = new comm();

	RTM = new rtm();
	RTM.setToken( options.getValue("token") );
	if(COMM.connected){
		options_onOpen2();
	} else {
		COMM.testConnection(options_onOpen2);
	}
}

function options_onOpen2(){
	var token;

	RTM.checkToken();

	// Set checkboxes
	nodate.value = (options.getValue( "nodate" ) == "1" );
	todayontop.value = (options.getValue( "todayontop" ) == "1" );
	securehttp.value = (options.getValue( "securehttp" ) == "1" );
	completedtasks.value = (options.getValue("completedtasks") == "1" );
    startweekmonday.value = (options.getValue("usweek") == "0" );
	showdate.value = (options.getValue("showdate") == "1" );
	showtime.value = (options.getValue("showtime") == "1");
	showtags.value = (options.getValue("showtags") == "1"); 
	if (options.exists("futuredays"))
		days.value = options.getValue("futuredays")
	else
		days.value = "0";
	
	if (options.exists("sortby"))
		sortby.selectedIndex = parseInt( options.getValue("sortby"))
	else
		sortby.selectedIndex = 1;
	
	token = RTM.getTokenValue()
	if ( token.length == 0 ){
	
		nodate.visible = false;
		todayontop.visible = false;
		completedtasks.visible = false;
		label1.visible = false
		dayinfo.visible = false;
		days.visible = false;
		startweekmonday.visible = false;
		if(COMM.connected){
			options_onOpen3();
		} else {
			COMM.testConnection(options_onOpen3);
		}
		
	}
	else 
  {
		custinfo.visible = false;
		authinfo.visible = false;
		nodate.visible = true;
		todayontop.visible = true;
		securehttp.visible = true;
		startweekmonday.visible = true;
		completedtasks.visible = true;
		authlink.visible = false;
		label1.visible = true;
		dayinfo.visible = true;
		days.visible = true;
		sortby.visible = true;
		sortinfo.visible = true;
		showdate.visible = true;
		showtime.visible = true;
		showtags.visible = true;
		
		entry = "<listbox height=\"80\" name=\"lblists\" width=\"271\" x=\"10\" y=\"276\" background=\"#D4D0C8\" itemWidth=\"270\" itemHeight=\"20\"" 
		entry = entry + " itemOverColor=\"#30FFFFFF\" autoscroll=\"true\" itemSelectedColor=\"#30FFFFFF\"/>"
		divmain.AppendElement( entry );

		RTM.getLists( lists );
		loadLists(0);

		var colors = new Array("FF9484","849CE7","A5DE63","E7E7D6","FFB573","84EFF7","D6CE84","C6A5F7","A5CEC6","FFE773","000000","FFFFFF");
		for( var i=0; i<colors.length;++i){
			x = 20*i;
			entry = "<div enabled=\"true\" height=\"16\" width=\"16\" name=\"div" + colors[i] + "\" x=\"" + x + "\" y=\"0\" background=\"#" + colors[i] + "\" onclick=\"setListColor('#" + colors[i] + "');\"/>";
			colordiv.AppendElement(entry);
		}
	}
}

function options_onOpen3(){
	frob = RTM.getFrob();
	if( frob == null )
		frob = "";

	if(( frob.length == 0) || (!RTM.isConnected())){
		authlink.visible = false;
		authinfo.bold = true;
		authinfo.innerText = INFO_AUTHORIZENOTAVAIL;
	}
	else {
		if(!RTM.isConnected()){
			authlink.visible = false;
			authinfo.innerText = INFO_AUTHORIZENOTAVAIL;
		}
		else{
			authinfo.innerText = INFO_AUTHORIZE;
			authlink.visible = true;
		}
		authlink.href = RTM.createURL( "", ["frob=" + frob], "delete" );
	}
}

/***********************************************************************
 Color of lists
***********************************************************************/
function setListColor( color )
{
	i = lblists.selectedIndex;
	if( i > -1){	
		options.putValue( "color" + lists[i].id, color);
		loadLists(i);
	}
}

/***********************************************************************
	List visibility
***********************************************************************/
function setListShow( p, id ){
    var i = getListShow(id, p);
    
	if(i=="false")
		i="1"
	else
	    i = "0";
	
	options.putValue( "show" + id, i);

}

/***********************************************************************
	On Cancel
***********************************************************************/
function onCancel(){
	options.putValue("optionsSaved", 0);
}

/***********************************************************************
	On OK
***********************************************************************/
function onOk(){
	if(COMM.connected){
			onOk2();
		} else {
			COMM.testConnection(onOk2);
		}
}

function onOk2(){
	var d = new Date();

	RTM.getToken();
	options.putValue("token", RTM.getTokenValue());
  options.putValue("nodate", nodate.value ? "1" : "0");
  options.putValue("todayontop", todayontop.value ? "1" : "0");
	options.putValue("securehttp", securehttp.value ? "1" : "0");
	options.putValue("completedtasks", completedtasks.value ? "1" : "0");
	options.putValue("usweek", startweekmonday.value ? "0" : "1");
	options.putValue("showdate", showdate.value ? "1" : "0");
	options.putValue("showtime", showtime.value ? "1" : "0");
	options.putValue("showtags", showtags.value ? "1" : "0");
	options.putValue("futuredays", days.value);
	options.putValue("sortby", sortby.selectedIndex );
	options.putValue("optionsSaved", 1);
}

